<template>
    <view>
         <!-- 搜索框 -->
           <my-search bgcolor="orange" @click="gotoSearch"></my-search>
           <!-- 分类选择 -->
           <view class="cate-container">
             <!-- 左侧一级分类 -->
             <scroll-view class="cate-left" scroll-y :style="{height: wh+'px'}" >
               <view :class="['cate-left-item', active===i?'active':'']" v-for="(item,i) in cateList" :key="i" @click="changeActive(i)">{{item.cat_name}}</view>
             </scroll-view>
             
             
             <!-- 右侧二级分类 -->
            			<scroll-view class="cate-right" :style="{height: wh+'px'}" scroll-y :scroll-top="scrollTop">
            				<view class="cate-right-item" v-for="(item, i) in cateLevel2" :key="i">
            					<!-- 二级分类标题 -->
            					<view class="cate-right-item-title">/{{item.cat_name}}/</view>
            					<!-- 二级分类具体内容 -->
            					<view class="cate-right-item-content">
            						<view class="cate-right-item-content-box" v-for="(item2, i2) in item.children" :key="i2" @click="gotoGoodsList(item2)">
            							<image :src="item2.cat_icon" mode="widthFix"></image>
            							<text>{{item2.cat_name}}</text>
            						</view>
            					</view>
            				</view>
            			</scroll-view>
                        
           </view>
    </view>
</template>

<script>
  // 导入自己封装的 mixin 模块
  import badgeMix from '@/mixins/setBadge.js'
  export default {
      // 将 badgeMix 混入到当前的页面中进行使用
        mixins: [badgeMix],
        data() {
            return {
                // 分类数据
                cateList:[],
                // 二级分类数据
                cateLevel2:[],
                active:0,
                wh:0,
                // 滚动条距离顶部的距离
                    scrollTop: 0
            };
        },
        onLoad() {
            const sysInfo = uni.getSystemInfoSync()
            this.wh = sysInfo.windowHeight
            this.getCateList()
        },
        methods:{
            // 获取分类数据
           async getCateList(){
                const {data:res} = await uni.$http.get('/categories')
                // 数据异常判断
                if(res.meta.status !== 200) return uni.$showMsg()
                this.cateList = res.message
                // 获取二级分类内容
                this.cateLevel2=res.message[0].children
            },
            
            // 左侧分类点击事件
            changeActive(i){
                this.active = i
                this.cateLevel2 = this.cateList[i].children
                // 更新右侧二级分类距离顶部距离
                  this.scrollTop = Math.random()
            },
            // 点击二级分类跳转至商品列表页
            gotoGoodsList(cate) {
                uni.navigateTo({
                    url:`/subpkg/goods_list/goods_list?cid=${cate.cat_id}`
                })
            },
            gotoSearch(){
                uni.navigateTo({
                    url:'/subpkg/search/search'
                })
            }
            
            
        }
    }
</script>

<style lang="scss">
.cate-container {
  display: flex;
  
  
  .cate-left{
    width: 240rpx;
    .cate-left-item {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 120rpx;
      background-color: #efefef;
      font-size: 24rpx;
      // 选中样式
      &.active {
        display: flex;
        justify-content: space-between;
        background-color: #ffffff;
        // 左边红线
        &::before {
          content: '';
          width: 6rpx;
          height: 60rpx;
          background-color: #c00000;
        }
        // 右边白线和背景相同
        &::after {
          content: '';
          width: 6rpx;
          height: 60rpx;
          background-color: #fffff;
        }
      }
    }
  }
  
  
  .cate-right {
    .cate-right-item {
      .cate-right-item-title{
        display: flex;
        justify-content: center;
        font-weight: bold;
        font-size: 24rpx;
        padding: 20rpx 0;
      }
      .cate-right-item-content {
          display: flex;
          flex-wrap: wrap;
          .cate-right-item-content-box {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            width: 33.33%;
            padding: 20rpx;
            box-sizing: border-box;
            image {
            width: 100%;
                      }
            text {
            font-size: 24rpx;
                      }
          }
      }
      
    }
  }
}


</style>
